/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.widgets.layouts;

import java.util.HashMap;
import java.util.Map;

import dgbgui.gui.widgets.SpacerWidget;
import dgbgui.gui.widgets.WidthAdjustable;
import dgbgui.gui.widgets.visual.RectangleBorder;
import edu.umd.cs.piccolo.PNode;

@SuppressWarnings("serial")
public class HorizontalLayout extends Layout {
    
    public HorizontalLayout(PNode parent, double margin, double spacing) {
        super(parent,margin,spacing);
    }

    public void layoutChildren() {
        double xOffset = m_margin;
        double yOffset = m_margin;
        
        
        Map<SpacerWidget,Double> m_spacerWidhtMap = new HashMap<SpacerWidget,Double>();
        double widthWithoutSpacers = 0.0;
        double totalSpacerWeight = 0.0;
        // First determine the width without spacers. Also store
        // the spacer widgets to save multiple iterations
        for ( int index = 0; index < getChildrenCount(); ++index ) {
            PNode child = getChild(index);
            // Just store with a zero spacing value
            if ( child instanceof SpacerWidget ) {
                m_spacerWidhtMap.put((SpacerWidget)child,0.0);
                totalSpacerWeight += ((SpacerWidget)child).getWeight();
            }
            // Add to the total width if not a spacer
            if ( !( child instanceof SpacerWidget ) ) {
                widthWithoutSpacers += child.getFullBounds().getWidth() + m_spacing;
            }
        }
        // Subtract the last spacing and add the margin (*2)
        widthWithoutSpacers -= m_spacing;
        widthWithoutSpacers += 2*m_margin; 
        
        
        
        // If the width is less than the minimum,
        boolean addSpacing = false;
        double remainingSpace = 0.0;
        if ( getParent() instanceof RectangleBorder ) {
            double minParentWidth = ((WidthAdjustable)getParent()).getAbsoluteWidth();
            if ( widthWithoutSpacers < minParentWidth ) {
                addSpacing = true;
                remainingSpace = minParentWidth - widthWithoutSpacers;
            }
        }
        
        // If spacing is needed, calculate the space each spacer will occupy
        // This is done by taking the remaining space, and dividing it amongst
        // the spacers according to their weights.
        if ( addSpacing && (totalSpacerWeight > 0) ) {
            for ( SpacerWidget spacer : m_spacerWidhtMap.keySet() ) {
                m_spacerWidhtMap.put(spacer, spacer.getWeight()/totalSpacerWeight*remainingSpace);
            }
        }
        
        // Now we have all the info we need to do the layout
        for ( int index = 0; index < getChildrenCount(); ++index ) {
            PNode child = getChild(index);
            // A normal child, set the child position and add the width
            if (! ( child instanceof SpacerWidget ) ) {
                child.setOffset(xOffset - child.getX(), yOffset);
                xOffset += child.getFullBounds().getWidth() + m_spacing;
            }
            // For spacers, just add the calculated width to the offset
            else {
                xOffset += m_spacerWidhtMap.get((SpacerWidget)child);
            }
        }
        

    }


}
